/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.service.impl;

import me.zhengjie.domain.DocDepartment;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.DocDepartmentRepository;
import me.zhengjie.service.DocDepartmentService;
import me.zhengjie.service.dto.DocDepartmentDto;
import me.zhengjie.service.dto.DocDepartmentQueryCriteria;
import me.zhengjie.service.mapstruct.DocDepartmentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wcl
 * @website https://el-admin.vip
 * @description 服务实现
 * @date 2021-03-21
 **/
@Service
@RequiredArgsConstructor
public class DocDepartmentServiceImpl implements DocDepartmentService {

	private final DocDepartmentRepository docDepartmentRepository;
	private final DocDepartmentMapper docDepartmentMapper;

	@Override
	public Map<String, Object> queryAll(DocDepartmentQueryCriteria criteria, Pageable pageable) {
		Page<DocDepartment> page = docDepartmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
		return PageUtil.toPage(page.map(docDepartmentMapper::toDto));
	}

	@Override
	public List<DocDepartmentDto> queryAll(DocDepartmentQueryCriteria criteria) {
		return docDepartmentMapper.toDto(docDepartmentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
	}

	@Override
	@Transactional
	public DocDepartmentDto findById(Integer id) {
		DocDepartment docDepartment = docDepartmentRepository.findById(id).orElseGet(DocDepartment::new);
		ValidationUtil.isNull(docDepartment.getId(), "DocDepartment", "id", id);
		return docDepartmentMapper.toDto(docDepartment);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public DocDepartmentDto create(DocDepartment resources) {
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		resources.setGmtCreate(timestamp);
		resources.setGmtUpdate(timestamp);
		String username = SecurityUtils.getCurrentUser().getUsername();
		resources.setUserCreate(username);
		resources.setUserUpdate(username);
		return docDepartmentMapper.toDto(docDepartmentRepository.save(resources));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(DocDepartment resources) {
		DocDepartment docDepartment = docDepartmentRepository.findById(resources.getId()).orElseGet(DocDepartment::new);
		ValidationUtil.isNull(docDepartment.getId(), "DocDepartment", "id", resources.getId());
		docDepartment.copy(resources);
		docDepartment.setUserUpdate(SecurityUtils.getCurrentUser().getUsername());
		docDepartment.setGmtUpdate(new Timestamp(new Date().getTime()));
		docDepartmentRepository.save(docDepartment);
	}

	@Override
	public void deleteAll(Integer[] ids) {
		for (Integer id : ids) {
			docDepartmentRepository.deleteById(id);
		}
	}

	@Override
	public void download(List<DocDepartmentDto> all, HttpServletResponse response) throws IOException {
		List<Map<String, Object>> list = new ArrayList<>();
		for (DocDepartmentDto docDepartment : all) {
			Map<String, Object> map = new LinkedHashMap<>();
			map.put("科室名称", docDepartment.getName());
			map.put("创建时间", docDepartment.getGmtCreate());
			map.put("修改时间", docDepartment.getGmtUpdate());
			map.put("创建人", docDepartment.getUserCreate());
			map.put("修改人", docDepartment.getUserUpdate());
			list.add(map);
		}
		FileUtil.downloadExcel(list, response);
	}
}